<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div class="left"><Breadcrumb /></div>
        <div class="right">
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div v-loading="loading" class="main_main">
      <search-bar-new :parent="this" />
      <div ref="table_container" class="table_container">
        <el-table
          v-if="tableColumns.length>0"
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"

          :cell-style="cellStyle"
          @cell-click="cellClick"
          @selection-change="(val)=>{selectionChange(val, this)}"
          @sort-change="(column)=>{sortChange(column, this)}"
        >
          <el-table-column type="selection" width="70" align="center" />
          <Column v-for="(column,index) in tableColumns" :key="index" :align="column.align" :prop="column.prop" :is-show="column.isShow" :label="column.label" :sortable="column.sortable" :width="column.width" />
        </el-table>
      </div>
    </div>
    <div class="main_bottoms">
      <el-checkbox v-model="checked" @change="showZero">显示库存数量为0</el-checkbox>
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { getSparePartlist } from '@/api/equipmentManage/sparePart/outWarehouse'
import { list as warehouseList } from '@/api/equipmentManage/sparePart/warehouse'
import { importMethod } from '@/api/equipmentManage/sparePart/importSpare'
import { list as typeList } from '@/api/equipmentManage/sparePart/type'
import AddOrUpdate from './addOrUpdate'
export default {
  name: 'Index',
  data() {
    return {
      loading: true,
      pageId: '备件库存',
      pageExportId: '备件库存导出', // 页面导出标识
      pageData: {
        companyName: undefined,
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'updateTime',
        isAsc: 'desc',
        isAll: '1'
      },
      pageData2: {
        companyName: undefined,
        pageNum: 1,
        pageSize: 9999,
        orderByColumn: 'updateTime',
        isAsc: 'desc'
      },
      total: 1, // 数据一共有多少条
      mainId: 'id',
      tableHeight: '0',
      tableData: [],
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'spareCode', label: '备件编码' },
        { prop: 'spareName', label: '备件名称', canOrder: false },
        { prop: 'spareModel', label: '规格型号', canOrder: false },
        { prop: 'supplierName', label: '备件供应商', canOrder: false },
        { prop: 'spareTypeName', label: '备件类型', canOrder: false },
        { prop: 'numberUnitLabel', label: '备件单位', canOrder: false },
        { prop: 'safeInventory', label: '安全库存', canOrder: false },
        { prop: 'weightedAveragePriceThousandths', label: '备件单价(元)', canOrder: false, align: 'right' },
        { prop: 'number', label: '库存数量', canOrder: false },
        { prop: 'deviceSpareWarehouseName', label: '仓库', canOrder: false }
      ],
      exportTableColumns: [// 表格常规字段（初始值）
        { prop: 'spareCode', label: '备件编码' },
        { prop: 'spareName', label: '备件名称', canOrder: false },
        { prop: 'spareModel', label: '规格型号', canOrder: false },
        { prop: 'supplierName', label: '备件供应商', canOrder: false },
        { prop: 'spareTypeName', label: '备件类型', canOrder: false },
        { prop: 'numberUnitLabel', label: '备件单位', canOrder: false },
        { prop: 'safeInventory', label: '安全库存', canOrder: false },
        { prop: 'weightedAveragePriceThousandths', label: '备件单价(元)', canOrder: false, align: 'right' },
        { prop: 'number', label: '库存数量', canOrder: false },
        { prop: 'deviceSpareWarehouseName', label: '仓库', canOrder: false }
      ],
      defaultExport: [],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'searchValue', label: '模糊搜索', type: 'input' },
        // { prop: 'spareCode', label: '备件编码', type: 'input' },
        // { prop: 'spareName', label: '备件名称', type: 'input' },
        // { prop: 'spareModel', label: '规格型号', type: 'input' },
        // { prop: 'supplierName', label: '备件供应商', type: 'input' },
        { prop: 'spareType', label: '备件类型', type: 'select', optionsName: 'statusOptions' },
        { prop: 'deviceSpareWarehouseId', label: '仓库', type: 'select', optionsName: 'statusOptionsWarehouse' },
        { prop: 'safeStock', label: '安全库存', type: 'select', optionsName: 'statusSafe' }
        // { prop: 'numberUnitLabel', label: '备件单位', type: 'input' }
      ],
      checked: true,
      operationData: [ // 右上角的操作按钮
        // { label: '新增', perm: '' },
        // { label: '批量删除', perm: '' },
        { label: '导入', perm: 'device:spareInventoryNumber:import' },
        { label: '批量导出3', perm: 'device:spareInventoryNumber:export' },
        { label: '页面配置', perm: '' }
      ],
      isAllList: [
        { label: '全部', value: undefined },
        { label: '不包含0', value: '0' }
      ],
      statusSafe: [
        { label: '全部', value: undefined },
        { label: '库存报警', value: '1' }
      ],
      statusOptions: [],
      statusOptionsWarehouse: [],
      allTableData: []
    }
  },
  computed: {
    // 分页器
    selectedNum() {
      return this.$refs.table ? this.$refs.table.selection.length : 0
    }
  },
  created() {
    this.getTypeList()
    this.loadWarehouseList()
    this.getLists()
  },
  methods: {
    cellStyle({ row, column }) {
      if (column.property == 'number') {
        return {
          cursor: 'pointer',
          color: '#387bfc'
        }
      }
    },
    cellClick(row, column, cell) {
      switch (column.property) {
        case 'number':
          this.detail(row)
          break
      }
    },
    showZero(e) {
      e ? this.pageData.isAll = '1' : this.pageData.isAll = '0'
      this.getList()
    },
    importMethod(files) { // 导入接口
      console.log(files)
      importMethod(files).then(res => {
        if (res.code == 200) {
          this.$message.success('导入成功')
          this.getList()
        }
      })
    },
    getList() {
      getSparePartlist(this.pageData).then(res => {
        this.tableData = res.data
        this.total = res.total
        this.loading = false
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
          this.tableHeight = this.$refs.table_container.offsetHeight
        })
      })
    },
    getLists() {
      getSparePartlist(this.pageData2).then(res => {
        this.allTableData = res.data
      })
    },
    getTypeList() {
      typeList({ status: 1 }).then(res => {
        res.data.forEach(item => {
          item['value'] = item.id
          item['label'] = item.name
        })
        res.data.unshift({ value: undefined, label: '全部' })
        this.statusOptions = res.data
      })
    },
    detail(row) {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            deviceSpareWarehouseId: row.deviceSpareWarehouseId,
            deviceSpareStandingBookId: row.deviceSpareStandingBookId
          }// props
        },
        area: ['880px', '570px'],
        title: '库存明细', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    getExportData(data) {
      return getSparePartlist(data)
    },
    loadWarehouseList() {
      warehouseList({ status: 1 }).then(res => {
        res.data.forEach(item => {
          item['value'] = item.id
          item['label'] = item.name
        })
        res.data.unshift({ value: undefined, label: '全部' })
        this.statusOptionsWarehouse = res.data
      })
    }
  }
}
</script>

<style lang="less" scoped>
  .main_bottoms{
    width: 100%;
    height: 50px;
    padding: 0px 20px;
    display: flex;justify-content: space-between;align-items: center;
  }
</style>
